package com.fddh.springboot.service.impl;

import com.fddh.springboot.dto.NewUserDto;
import com.fddh.springboot.dto.UserDto;
import com.fddh.springboot.entity.User;
import com.fddh.springboot.mapper.UserMapper;
import com.fddh.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("UserServiceImpl")    //注释为服务，指定bean名称为LoginServiceImpl
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    //get all user
    @Override
    public List<User> getUserList() {
        List<User> list = new ArrayList<>();

        list = userMapper.getUserList();
        return list;
    }

    //add user
    @Override
    public int addUser(NewUserDto newuserDto) {

        userMapper.addUser(newuserDto);
        //检查是否添加进去,如果存在这个用户不为空，则已经添加进去 return 1
        int FLAG = 0;
        if(userMapper.getUserByName(newuserDto.getUsername())!=null)//
        {
            FLAG = 1;
        }
        else{
            FLAG = 0;
        }
        return FLAG;
    }

    //delete user deleteUser
    @Override
    public int deleteUser(UserDto userDto) {
        userMapper.deleteUser(userDto.getUsername());
        int flag = 0;
        if(userMapper.getUserByName(userDto.getUsername())==null){
            //不在有这个人，则删除成功
            flag = 1;
        }
        return flag;
    }

    //修改后台会员用户信息
    @Override
    public void updateUser(NewUserDto newUserDto) {

        userMapper.updateUser(newUserDto);

    }

}
